﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using Utility.Tool;
using System.IO;

namespace Utility.Images
{
    /// <summary>
    /// 图片操作类
    /// </summary>
    public class ImageHelper
    {
        #region 生成缩略图
        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="thumbnailPath">缩略图路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>    
        /// <param name="picExtension">图片后缀[不加点]</param>
        public static bool MakeThumbnail(Image originalImage, string thumbnailPath, int width, int height, Zoom mode, string picExtension)
        {
            bool result = false;
            if (originalImage == null) return result;

            int towidth = width;
            int toheight = height;

            int x = 0;
            int y = 0;
            int ow = originalImage.Width;
            int oh = originalImage.Height;
            string path = thumbnailPath.Remove(thumbnailPath.LastIndexOf("\\"));
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            switch (mode)
            {
                case Zoom.HW://指定高宽缩放（可能变形）                
                    break;
                case Zoom.W://指定宽，高按比例                    
                    toheight = originalImage.Height * width / originalImage.Width;
                    break;
                case Zoom.H://指定高，宽按比例
                    towidth = originalImage.Width * height / originalImage.Height;
                    break;
                case Zoom.Cut://指定高宽裁减（不变形）                
                    if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
                    {
                        oh = originalImage.Height;
                        ow = originalImage.Height * towidth / toheight;
                        y = 0;
                        x = (originalImage.Width - ow) / 2;
                    }
                    else
                    {
                        ow = originalImage.Width;
                        oh = originalImage.Width * height / towidth;
                        x = 0;
                        y = (originalImage.Height - oh) / 2;
                    }
                    break;
                default:
                    break;
            }
            Image bitmap = new Bitmap(towidth, toheight);
            Graphics g = Graphics.FromImage(bitmap);
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),
                new Rectangle(x, y, ow, oh),
                GraphicsUnit.Pixel);

            try
            {
                if (!picExtension.StartsWith(".")) picExtension = "." + picExtension;

                switch (picExtension.ToLower())
                {
                    case ".jpg":
                    case ".jpeg":
                    case ".pjpeg":
                        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                    case ".gif":
                        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    case ".bmp":
                        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Bmp);
                        break;
                    case ".png":
                        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    default:
                        bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                }
                result = true;
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                originalImage.Dispose();
                bitmap.Dispose();
                g.Dispose();
            }
            return result;
        }

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="picStream">原图流</param>
        /// <param name="thumbnailPath">缩略图路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>    
        /// <param name="picExtension">图片后缀[不加点]</param>
        public static bool MakeThumbnail(Stream picStream, string thumbnailPath, int width, int height, Zoom mode, string picExtension)
        {
            Image originalImage = Image.FromStream(picStream);
            return MakeThumbnail(originalImage, thumbnailPath, width, height, mode, picExtension);
        }

        /// <summary>
        /// 生成缩略图
        /// </summary>
        /// <param name="originalImagePath">源图路径（物理路径）</param>
        /// <param name="thumbnailPath">缩略图路径（物理路径）</param>
        /// <param name="width">缩略图宽度</param>
        /// <param name="height">缩略图高度</param>
        /// <param name="mode">生成缩略图的方式</param>    
        public static bool MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, Zoom mode)
        {
            bool result = false;
            using (Image originalImage = Image.FromFile(originalImagePath))
            {
                FileInfo f = new FileInfo(originalImagePath);
                result = MakeThumbnail(originalImage, thumbnailPath, width, height, mode, f.Extension);
            }
            return result;
        }

        #endregion
    }

    /// <summary>
    /// 
    /// </summary>
    public enum Zoom
    {
        /// <summary>
        /// 
        /// </summary>
        HW,
        /// <summary>
        /// 
        /// </summary>
        H,
        /// <summary>
        /// 
        /// </summary>
        W,
        /// <summary>
        /// 
        /// </summary>
        Cut
    }
}
